package com.probox.common.core.utils.money;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class DecryptUtil {
    protected final static Logger logger = LoggerFactory.getLogger(DecryptUtil.class);

    public static String decryptS5(String sSrc, String encodingFormat, String sKey, String ivParameter) throws Exception {
        //BASE64Decoder decoder = new BASE64Decoder();
        logger.info("sSrc=>"+sSrc);
        logger.info("encodingFormat=>"+encodingFormat);
        logger.info("sKey=>"+sKey);
        logger.info("ivParameter=>"+ivParameter);
        Base64.Decoder decoder = Base64.getDecoder();
        try {
            byte[] raw = decoder.decode(sKey);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            IvParameterSpec iv = new IvParameterSpec(decoder.decode(ivParameter));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] myendicod = decoder.decode(sSrc);
            byte[] original = cipher.doFinal(myendicod);
            String originalString = new String(original, encodingFormat);
            return originalString;
        } catch (Exception ex) {
            ex.printStackTrace();
            AesUtil.decrypt(decoder.decode(sSrc), decoder.decode(sKey),
                    decoder.decode(ivParameter), "utf-8");
            //decrypt(decoder.decodeBuffer(sKey),decoder.decodeBuffer(ivParameter),decoder.decodeBuffer(sSrc));
        }
        return null;
    }
}
